/**
 * Created by zy on 2016/6/13.
 * 变化趋势/数据对比分析(共用) - 管体缺陷仪表板 - 管体缺陷
 */

//腐蚀控制-阴保运行管理-异常处理

;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
    //所属场站阀室
    var $chamber_station = $('#JQ_chamber_station').select3({
        title: '所属场站阀室',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });

    //异常管理表格
    var $room_table = $('#JQ_datatable_01');
    // 查询
    var $search = $('#JQ_search');
    // 导出
    var $expect = $('#JQ_export_01');

    //处置状态
    var $Disposal_status = $('#JQ_Disposal_status').select3({search: false});

    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
            //场站阀室
            DyyBase.ajax(DyyBase.url_sign(), {pipeLineId: id, targetType: 'valveRoomStation'}, function (dataset) {
                var valveRoom = DyyBase.getSelect(dataset);
                $chamber_station.asset(valveRoom);
            });

        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
            $chamber_station.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });

    var filter = {
        // 异常管理
        getExceptionFilter: function (data) {
            var filter = {};
            // 分页参数
            if (data) {
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            //查询参数
            filter.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            filter.beginId = function () {
                var v = $begin.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            filter.endId = function () {
                var v = $end.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            filter.pipeValveroomId = function () {
                var v = $chamber_station.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            filter.handlingState = function () {
                var v = $Disposal_status.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            filter.eqptId = function (){
                var v = $('#eqptId').attr('data_value');
                if (v) {
                    return v;
                }
                return '';
            }();
            filter.pipeName=function(){
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            return filter;
        },
        // 执行查询后的筛选对象
        getExportFilter: function(){
            return filter.getExceptionFilter();
        }
    };

    /*** 加载列表 start ***/
    var DTROOM = $room_table.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.defect_empty('报告领导，暂时没有数据 ！','换别的条件查询吧！'),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getExceptionFilter(data);
            DyyBase.ajax(corrctrlApi.url_exception_search(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
        },
        'columns': [
            {
                data: 'pipeLineName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'teststationnumber', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'pipeValveroomName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'eqptType_label', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'eqptNumber', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'description', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'stated', render: function (data) {
                console.log(data)
                var color = '';
                if(data=="未处理"){
                    color='lsk-no-color';
                }
                return '<span title="'+data+'" class="' + color + '">'+data+'</span>';
            }
            },
            {
                data: 'recordTime', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'id', render: function (data) {
                return '<a href="'+DyyBase.root_path+'/corrctrl/eqptException/recoedExceptionInfo.html?exceptionId=' + data + '"><i class="lsk-yc-icon"></i></a>';
            }
            }
        ]
    },{async:false});

    // 查询
    $('#JQ_search').click(function () {
        if (DTROOM) {
            DTROOM.draw();
            filter.getExportFilter = filter.getExceptionFilter();
        }
    });
    //    导出
    $expect.click(function () {
        //var param = filter.getExportFilter;
        var param = filter.getExportFilter;
        if(param instanceof Function){
            param = param();
        }
        $(this).EXPT({
            name: param.pipeName+'异常管理信息表',
            param: param,
            server: DyyBase.root_path +　'/corrctrl/eqptException/queryEqptRecoedExceptionExl.json'
        });
    })


})(jQuery, window, document);